package com.ht.ui.report;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JRViewer;

import javax.swing.*;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Dong Shufeng
 * Date: 11-10-29
 */
public class SimpleAnalogJRMaker2 extends AbstractAnalogJRMaker {
    private List<Integer> measureIds;
    private List<String> displayNames;
    private int rowCount = 0;
    private int index = -1;

    public SimpleAnalogJRMaker2() {
    }

    public SimpleAnalogJRMaker2(URL resource, int mode) {
        super(resource, mode);
    }

    public SimpleAnalogJRMaker2(List<Integer> measureIds, List<String> displayNames) {
        this.measureIds = measureIds;
        this.displayNames = displayNames;
    }

    public SimpleAnalogJRMaker2(URL resource, int mode, List<Integer> measureIds, List<String> displayNames) {
        super(resource, mode);
        this.measureIds = measureIds;
        this.displayNames = displayNames;
    }

    public JComponent createViewer(long start, long end) {
        JasperReport jasperReport;
        HashMap<String, Object> pMap = new HashMap<String, Object>();
        pMap.put("date", new Date(start));
        try {
            jasperReport = (JasperReport) JRLoader.loadObject(resource);
        } catch (JRException e) {
            e.printStackTrace();
            return null;
        }
        JasperPrint jasperPrint;
        try {
            if (measureIds != null && measureIds.size() > 0) {
                int[] ids = new int[measureIds.size()];
                for (int i = 0; i < measureIds.size(); i++)
                    ids[i] = measureIds.get(i);
                loadValueFormDB(ids, new Date(start));
            }
            rowCount = 0;
            index = -1;
            jasperPrint = JasperFillManager.fillReport(jasperReport, pMap, this);
        } catch (JRException e) {
            e.printStackTrace();
            return null;
        }
        JRViewer viewer = new JRViewer(jasperPrint);
        viewer.setFitPageZoomRatio();
        prints.put(viewer, jasperPrint);
        return viewer;
    }

    public boolean next() throws JRException {
        index++;
        if (measureIds == null || measureIds.size() < 1)
            return false;
        boolean b = false;
        if (rowCount < measureIds.size())
            b = true;
        rowCount++;
        return b;
    }

    public Object getFieldValue(JRField jrField) throws JRException {
        if (jrField.getName().equals("name")) {
            return displayNames.get(index);
        } else if (jrField.getName().equals("value")) {
            if (nId2Values.containsKey(measureIds.get(index)))
                return nId2Values.get(measureIds.get(index));
            else
                return null;
        }
        return null;
    }

    public void setMeasureIds(List<Integer> measureIds) {
        this.measureIds = measureIds;
    }

    public void setDisplayNames(List<String> displayNames) {
        this.displayNames = displayNames;
    }
}
